/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file    can.c
  * @brief   This file provides code for the configuration
  *          of the CAN instances.
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2024 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "can.h"

/* USER CODE BEGIN 0 */

#define CAN1_FILTER_BANK 0
#define CAN2_FILTER_BANK 14

CAN_TxHeaderTypeDef hCAN1_TxHeader = /* CAN1发送报文 */
{
  .StdId    = 0x100,  /* 11位  标准标识符 */
  .ExtId    = 0,      /* 29位  扩展标识符 */
  .IDE      = 0,      /* 1位   0:标准帧 1:拓展帧 */
  .RTR      = 0,      /* 1位   0:数据帧 1:远程帧 */
  .DLC      = 8,      /* 4位   发送的数据的长度 */
  .TransmitGlobalTime = ENABLE
};

rt_uint8_t tx_data_field[8] = {0};
rt_uint8_t rx_data_field[8] = {0};

/* USER CODE END 0 */

CAN_HandleTypeDef hcan1;

/* CAN1 init function */
void MX_CAN1_Init(void)
{

  /* USER CODE BEGIN CAN1_Init 0 */

  /* USER CODE END CAN1_Init 0 */

  /* USER CODE BEGIN CAN1_Init 1 */

  /* USER CODE END CAN1_Init 1 */
  hcan1.Instance = CAN1;
  hcan1.Init.Prescaler = 6;
  hcan1.Init.Mode = CAN_MODE_NORMAL;
  hcan1.Init.SyncJumpWidth = CAN_SJW_1TQ;
  hcan1.Init.TimeSeg1 = CAN_BS1_11TQ;
  hcan1.Init.TimeSeg2 = CAN_BS2_2TQ;
  hcan1.Init.TimeTriggeredMode = DISABLE;
  hcan1.Init.AutoBusOff = DISABLE;
  hcan1.Init.AutoWakeUp = DISABLE;
  hcan1.Init.AutoRetransmission = DISABLE;
  hcan1.Init.ReceiveFifoLocked = DISABLE;
  hcan1.Init.TransmitFifoPriority = DISABLE;
  if (HAL_CAN_Init(&hcan1) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN CAN1_Init 2 */

  CAN1Filter_Config();

  /*-3- 启动CAN ---------------------------------------------------*/
  while(HAL_CAN_Start(&hcan1) != HAL_OK )
  {
    rt_kprintf("\nCAN_Start Failed!!");
    rt_thread_delay(100);
  }
  /* USER CODE END CAN1_Init 2 */

}

void HAL_CAN_MspInit(CAN_HandleTypeDef* canHandle)
{

  GPIO_InitTypeDef GPIO_InitStruct = {0};
  if(canHandle->Instance==CAN1)
  {
  /* USER CODE BEGIN CAN1_MspInit 0 */

  /* USER CODE END CAN1_MspInit 0 */
    /* CAN1 clock enable */
    __HAL_RCC_CAN1_CLK_ENABLE();

    __HAL_RCC_GPIOA_CLK_ENABLE();
    /**CAN1 GPIO Configuration
    PA11     ------> CAN1_RX
    PA12     ------> CAN1_TX
    */
    GPIO_InitStruct.Pin = GPIO_PIN_11|GPIO_PIN_12;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
    GPIO_InitStruct.Alternate = GPIO_AF9_CAN1;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

  /* USER CODE BEGIN CAN1_MspInit 1 */

  /* USER CODE END CAN1_MspInit 1 */
  }
}

void HAL_CAN_MspDeInit(CAN_HandleTypeDef* canHandle)
{

  if(canHandle->Instance==CAN1)
  {
  /* USER CODE BEGIN CAN1_MspDeInit 0 */

  /* USER CODE END CAN1_MspDeInit 0 */
    /* Peripheral clock disable */
    __HAL_RCC_CAN1_CLK_DISABLE();

    /**CAN1 GPIO Configuration
    PA11     ------> CAN1_RX
    PA12     ------> CAN1_TX
    */
    HAL_GPIO_DeInit(GPIOA, GPIO_PIN_11|GPIO_PIN_12);

  /* USER CODE BEGIN CAN1_MspDeInit 1 */

  /* USER CODE END CAN1_MspDeInit 1 */
  }
}

/* USER CODE BEGIN 1 */

/* CAN1 滤波器设置 */
void CAN1Filter_Config(void)
{
    CAN_FilterTypeDef  sFilterConfig;

    sFilterConfig.FilterBank = CAN1_FILTER_BANK;        /* CAN过滤器编号，范围0-27 */
    sFilterConfig.FilterMode = CAN_FILTERMODE_IDMASK;   /* CAN过滤器模式，掩码模式或列表模式 */
    sFilterConfig.FilterScale = CAN_FILTERSCALE_32BIT;  /* CAN过滤器尺度，16位或32位 */
    sFilterConfig.FilterIdHigh = 0x000 << 5;            /* 32位下，存储要过滤ID的高16位 */
    sFilterConfig.FilterIdLow = 0x0000;                 /* 32位下，存储要过滤ID的低16位 */
    sFilterConfig.FilterMaskIdHigh = 0x0000;            /* 掩码模式下，存储的是掩码 */
    sFilterConfig.FilterMaskIdLow = 0x0000;
    sFilterConfig.FilterFIFOAssignment = CAN_RX_FIFO0;  /* 报文通过过滤器的匹配后，存储到哪个FIFO */
    sFilterConfig.FilterActivation = ENABLE;    		    /* 激活过滤器 */
    sFilterConfig.SlaveStartFilterBank = CAN2_FILTER_BANK;

  if (HAL_CAN_ConfigFilter(&hcan1, &sFilterConfig) != HAL_OK)
  {
    Error_Handler();
  }
}

rt_bool_t can1_send_frame(rt_uint32_t id, rt_uint8_t* msg, rt_uint32_t size)
{
  rt_bool_t retval = RT_FALSE;
  uint32_t Tx_MailBox = 0;

  hCAN1_TxHeader.IDE = 0;
  hCAN1_TxHeader.StdId = id;
  hCAN1_TxHeader.ExtId = 0;
  hCAN1_TxHeader.DLC = size;  /* 4bit 发送的数据的长度 */
  hCAN1_TxHeader.RTR = 0;     /* 1bit 0:数据帧 1:远程帧 */
  hCAN1_TxHeader.TransmitGlobalTime = ENABLE;

  rt_memmove(tx_data_field, msg, size);

  if (HAL_OK == HAL_CAN_AddTxMessage(&hcan1, &hCAN1_TxHeader, tx_data_field, &Tx_MailBox))
  {
    retval = RT_TRUE;
  }
  else
  {
    retval = RT_FALSE;
  }

  return retval;
}

rt_bool_t can1_receive_frame(rt_uint32_t* id, rt_uint8_t* msg, rt_uint32_t* size)
{
  rt_bool_t retval = RT_FALSE;
  CAN_RxHeaderTypeDef hCAN1_RxHeader; /* CAN1接收报文 */

  if(HAL_OK == HAL_CAN_GetRxMessage(&hcan1, CAN_RX_FIFO0, &hCAN1_RxHeader, rx_data_field))
  {
    *id = hCAN1_RxHeader.StdId;
    *size = hCAN1_RxHeader.DLC;
    rt_memmove(msg, rx_data_field, hCAN1_RxHeader.DLC);
    retval = RT_TRUE;
  }
  else if (HAL_OK == HAL_CAN_GetRxMessage(&hcan1, CAN_RX_FIFO1, &hCAN1_RxHeader, rx_data_field))
  {
    *id = hCAN1_RxHeader.StdId;
    *size = hCAN1_RxHeader.DLC;
    rt_memmove(msg, rx_data_field, hCAN1_RxHeader.DLC);
    retval = RT_TRUE;
  }
  else{
    retval = RT_FALSE;
  }
  return retval;
}

/* USER CODE END 1 */
